package com.nopay.cdxphone.ui.fragment;

import android.graphics.Bitmap;
import android.text.TextUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseFragment;
import com.nopay.cdxphone.bean.response.UserStatisticsResponse;
import com.nopay.cdxphone.dialog.DialogWarmPrompt;
import com.nopay.cdxphone.utils.MemberManager;
import com.nopay.cdxphone.wx.WXSDKManager;
import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.FragmentStatisticsBinding;

import com.nopay.cdxphone.ui.ativity.MainActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: guizhou_assets
 * @Package: dhc.guizhou.assets.view.main.fragment
 * @ClassName: 首页
 * @Description: java类作用描述
 * @CreateDate: 2020-5-18 13:57
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020-5-18 13:57
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class StatisticsFragment extends BaseFragment<FragmentStatisticsBinding> {

    @Override
    protected void loadData() {
        binding.statusBar.setPadding(0, getStatusBarHeight(getActivity()), 0, 0);

        boolean userFromLogin = SPUtils.getInstance().getBoolean(Constants.SP_USER_FROM_LOGIN, true);
        if(userFromLogin) new DialogWarmPrompt(getActivity()).show();

        binding.layoutTextCourse.setOnClickListener(view -> {
            // TODO 图文教程
            ((MainActivity) getActivity()).gotoTextCourse();
        });

        binding.layoutVideoCourse.setOnClickListener(view -> {
            // TODO 视频教程
            ((MainActivity) getActivity()).gotoVideoCourse();
        });

        binding.layoutProblemAnswer.setOnClickListener(view -> {
            // TODO 疑难处理
            ((MainActivity) getActivity()).gotoProblemAnswer();
        });

        binding.layoutTodayScreenshot.setOnClickListener(view -> {
            // TODO 今日报表
            binding.rlRoot.destroyDrawingCache();
            binding.rlRoot.setDrawingCacheEnabled(true);
            binding.rlRoot.buildDrawingCache();
            Bitmap bitmap = binding.rlRoot.getDrawingCache();
            WXSDKManager.share(bitmap);
        });

        getUserStatistics();
    }

    public void refreshData() {
        if(binding != null) getUserStatistics();
    }

    private void getUserStatistics() {
        String phoneNum = SPUtils.getInstance().getString(Constants.SP_PHONE, "");
        api.doGetUserStatistics(phoneNum).enqueue(new Callback<UserStatisticsResponse>() {
            @Override
            public void onResponse(Call<UserStatisticsResponse> call, Response<UserStatisticsResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        UserStatisticsResponse.Data data = response.body().getData();
                        MemberManager.saveUserTimes(data.getTodayCallCount());

                        // TODO 今日累计
                        binding.tvTodayCallTimes.setText(disposeNull(String.valueOf(data.getTodayCallCount()), "次"));
                        binding.tvTodayConnectionTimes.setText(disposeNull(String.valueOf(data.getTodayCallSuccessCount()), "次"));
                        binding.tvTodayDisconnectTimes.setText(disposeNull(String.valueOf(data.getTodayCallFailCount()), "次"));
                        binding.tvTodayDuration.setText(disposeNull(String.valueOf(data.getTodayTotalDuration()), "min"));
                        binding.tvTodayAverageDuration.setText(disposeNull(String.valueOf(data.getTodayAvgDuration()), "s"));

                        // TODO 本月累计
                        binding.tvMonthCallTimes.setText(disposeNull(String.valueOf(data.getMonthCallCount()), "次"));
                        binding.tvMonthConnectionTimes.setText(disposeNull(String.valueOf(data.getMonthCallSuccessCount()), "次"));
                        binding.tvMonthDisconnectTimes.setText(disposeNull(String.valueOf(data.getMonthCallFailCount()), "次"));
                        binding.tvMonthDuration.setText(disposeNull(String.valueOf(data.getMonthTotalDuration()), "min"));
                        binding.tvMonthAverageDuration.setText(disposeNull(String.valueOf(data.getMonthAvgDuration()), "s"));
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<UserStatisticsResponse> call, Throwable t) {
                ToastUtils.showShort(getActivity().getResources().getString(R.string.call_api_error));
            }
        });
    }

    private static String disposeNull(String value, String unit) {
        if (TextUtils.isEmpty(value) || "null".equals(value)){
            return "-";
        } else {
            return value + unit;
        }
    }

}
